package com.ifp.opengate.boot.bean.access;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.ifp.opengate.boot.bean.config.ConfigBean;

/**
 * Created by mogs on 2018/9/1.
 */
public class AccessBean {

    protected long accessCount = 0;

    // 访问时间
    protected long accessTime;

    private boolean allowAccess;

    private ConfigBean configBean;

    /** 访问统计 */
    private Map<String, AccessBean> subAccessBeanMap = new ConcurrentHashMap<String, AccessBean>();

    public AccessBean() {

    }

    public AccessBean(ConfigBean configBean) {
        this.configBean = configBean;
    }

    public synchronized long count(int type) {
        switch (type) {
            case 1:
                ++accessCount;
                break;
            case 2:
                accessCount = --accessCount > 0 ? accessCount : 0;
                break;
        }
        return accessCount;
    }

    public synchronized long count(int type, long length) {
        switch (type) {
            case 1:
                accessCount += length;
                break;
            case 2:
                accessCount = accessCount - length > 0 ? accessCount : 0;
                break;
        }
        return accessCount;
    }

    public synchronized void clearCount() {
        if (System.currentTimeMillis() > (this.getAccessTime() + this.getConfigBean().getInterval())) {
            accessCount = 0;
            accessTime = System.currentTimeMillis();
        }
    }

    public long getMax() {
        return this.configBean.getMax();
    }

    public long getInterval() {
        return this.configBean.getInterval();
    }

    public long getAccessCount() {
        return accessCount;
    }

    public void setAccessCount(long accessCount) {
        this.accessCount = accessCount;
    }

    public long getAccessTime() {
        return accessTime;
    }

    public void setAccessTime(long accessTime) {
        this.accessTime = accessTime;
    }

    public boolean isAllowAccess() {
        ConfigBean configBean = this.getConfigBean();
        // 如果没配置，默认不控制
        if (configBean == null) {
            this.setAllowAccess(true);
            return allowAccess;
        }
        if (this.accessTime == 0) {
            this.setAccessTime(System.currentTimeMillis());
        }
        if (this.getAccessCount() >= configBean.getMax()) {
            this.clearCount();
        }
        if (this.getAccessTime() > 0 && System.currentTimeMillis() <= (this.getAccessTime() + configBean.getInterval())
            && this.getAccessCount() >= configBean.getMax()) {
            this.setAllowAccess(false);
        } else {
            this.setAllowAccess(true);
        }
        return allowAccess;
    }

    public void setAllowAccess(boolean allowAccess) {
        this.allowAccess = allowAccess;
    }

    public ConfigBean getConfigBean() {
        return configBean;
    }

    public void setConfigBean(ConfigBean configBean) {
        this.configBean = configBean;
    }

    public Map<String, AccessBean> getSubAccessBeanMap() {
        return subAccessBeanMap;
    }

    public void setSubAccessBeanMap(Map<String, AccessBean> subAccessBeanMap) {
        this.subAccessBeanMap = subAccessBeanMap;
    }
}
